<template>
	<view class="address_container">
		<view class="address_list">
			<view class="address_item" v-for="item in addressList" :key="item.id" @click.stop="confirmAddress(item.id)">
				<view class="address_info">
					<view class="person">
						<view class="name">{{item.conPer}}</view>
						<view class="phone">{{item.phoneNumber}}</view>
					</view>
					<view class="address">
						{{item.province}}{{item.city}}{{item.disCoun.split(",")[0]}}{{item.detaAddress}}</view>
				</view>
				<view class="handle">
					<view @click.stop="addEditHandle(item)">
						<u-icon name="edit-pen" color="#9E9E9E" size="18" ></u-icon>
					</view>
					<view @click.stop="delHandle(item.id)">
						<u-icon name="trash" color="#9E9E9E" size="18" ></u-icon>
					</view>
				</view>
				<view class="tip" v-if="item.isDefault">默认</view>
			</view>
		</view>
		<!-- 删除弹框 -->
		<u-modal :show="delFlag" :content='content' :showCancelButton="true" @confirm="delConfirm" @cancel="cancel">

		</u-modal>
		<view class="submit_container">
			<view class="submit" @click="addEditHandle()">添加收货地址</view>
		</view>
	</view>
</template>

<script>
	import {
		getAddressList,
		delAddress
	} from "@/api/address.js"
	export default {
		data() {
			return {
				backUrl: "",
				addressList: [],
				currentId: "",
				delFlag: false,
				content: "确定删除吗？",
				currentId: null
			}
		},
		onLoad(option) {
			this.backUrl = ""
			console.log("option", option);
			if (option.type == "addInvoice") {
				this.backUrl = '/pagesA/pages/addInvoice/addInvoice'
			}
			if (option.type == "invoice") {
				this.backUrl = '/pagesA/pages/invoice/invoice'
			}
		},
		onShow() {
			this.getList()
		},
		methods: {
			// 获取列表数据
			async getList() {
				const res = await getAddressList()
				this.addressList = res
			},

			// 选择地址
			confirmAddress(val) {
				let pages = getCurrentPages(); // 当前页实例
				let nowPage = pages[pages.length - 1]; //当前页实例
				let prevPage = pages[pages.length - 2]; // 上一页面实例
				// 需要返回 上一页的数据 Object
				let object = {
					addressId: val
				};
				console.log(object);
				prevPage.$vm.prevDateFun(object) // 调用上一页 定义的方法
				// 返回 上一页
				uni.navigateBack({
					delta: 1 // 可以不写，默认值为 1
				})
			},

			// 添加/编辑地址
			addEditHandle(val) {
				if (val) {
					// 跳转到编辑页面
					uni.navigateTo({
						url: `/pagesA/pages/addAddress/addAddress?id=${val.id}`
					})
				} else {
					// 跳转到添加页面
					uni.navigateTo({
						url: `/pagesA/pages/addAddress/addAddress`
					})
				}
			},

			// 删除地址
			delHandle(val) {
				this.currentId = val
				this.delFlag = true
			},
			
			// 取消删除
			cancel(){
				this.delFlag = false
			},

			// 确定删除
			async delConfirm() {
				let list = [this.currentId]
				const res = await delAddress(list)
				if (res) {
					uni.showToast({
						title: '删除成功',
						icon: 'success',
						duration: 2000,
					})
					this.delFlag = false
					this.getList()
				} else {
					uni.showToast({
						title: '删除失败',
						icon: 'success',
						duration: 1000,
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.address_container {
		width: 100%;
		min-height: 100vh;
		background: rgb(245, 245, 245);
		padding-top: 6rpx;
		box-sizing: border-box;

		.address_list {
			padding-bottom: 170rpx;
		}

		.address_item {
			width: 706rpx;
			height: 130rpx;
			background: rgb(255, 255, 255);
			border-radius: 8rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-left: 28rpx;
			box-sizing: border-box;
			margin: 0 auto;
			margin-top: 28rpx;
			position: relative;

			.address_info {
				width: 75%;
				height: 76rpx;
				border-right: 1px solid rgb(214, 214, 214);

				.person {
					display: flex;
					align-items: flex-end;

					.name {
						color: rgb(0, 0, 0);
						font-family: 思源黑体;
						font-size: 26rpx;
						font-weight: 500;
					}

					.phone {
						color: rgb(120, 119, 119);
						font-family: 思源黑体;
						font-size: 20rpx;
						font-weight: 500;
						margin-left: 10rpx;
					}
				}

				.address {
					height: 34rpx;
					line-height: 34rpx;
					color: rgb(112, 111, 111);
					font-family: 思源黑体;
					font-size: 22rpx;
					font-weight: 400;
					margin-top: 5rpx;
				}
			}

			.handle {
				width: 25%;
				display: flex;
				align-items: center;
				justify-content: space-around;
			}

			.tip {
				position: absolute;
				right: 0;
				top: 0;
				width: 60rpx;
				height: 26rpx;
				line-height: 26rpx;
				text-align: center;
				background: rgb(223, 235, 253);
				border-radius: 0px 8rpx 0px 8rpx;
				color: rgb(60, 84, 184);
				font-family: 思源黑体;
				font-size: 18rpx;
				font-weight: 400;
			}
		}

		.submit_container {
			width: 100%;
			height: 162rpx;
			background: rgb(255, 255, 255);
			padding: 28rpx 22rpx;
			box-sizing: border-box;
			position: fixed;
			bottom: 0;

			.submit {
				height: 104rpx;
				background: rgb(62, 86, 186);
				border-radius: 16rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				color: rgb(255, 255, 255);
				font-family: 思源黑体;
				font-size: 40rpx;
				font-weight: 500;
			}
		}
	}
</style>